package p.ithorns.framework.logger.appender;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Context;
import ch.qos.logback.core.OutputStreamAppender;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import p.ithorns.framework.logger.stream.PipeLogOutputStream;

import java.io.IOException;
import java.io.PipedWriter;

/**
 * PipeLogAppender
 * 单Stream管道日志输出
 *
 * @author ithorns
 * @version 1.0.0
 * @date 2021/10/23 20:36
 */
public class PipeLogAppender extends OutputStreamAppender<ILoggingEvent> {

    private final static Logger log = LoggerFactory.getLogger(PipeLogAppender.class);

    private final PipeLogOutputStream outputStream;

    public PipeLogAppender() {
        this.outputStream = new PipeLogOutputStream( );
    }

    @Override
    public void setContext(Context context) {
        super.setContext(context);
        this.outputStream.setContext(this.context);
    }

    @Override
    public void start() {
        setOutputStream(this.outputStream);
        super.start();
    }

    @Override
    public void stop() {
        super.stop();
        try {
            this.outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void addWriter(PipedWriter writer) {
        this.outputStream.addWriter(writer);
    }

    public void removeWriter(PipedWriter writer) {
        this.outputStream.removeWriter(writer);
    }

}